package com.springboot.framework.controller;

/**
 * @author SWF
 * @date 2019/6/10 14:47
 **/

import com.github.pagehelper.PageInfo;
import com.springboot.framework.controller.response.PageResponseBean;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.OrderModel;
import com.springboot.framework.model.UserModel;
import com.springboot.framework.service.OrderService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(description = "【商家】订单")
@RestController
@RequestMapping("/seller/order")
public class SellerOrderController extends BaseController {

    @Resource
    private OrderService orderService;

    @ApiOperation(value = "查看订单列表", notes = "查看订单")
    @GetMapping("/listAll")
    public ResponseVO<PageResponseBean> listAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize, HttpServletRequest request) {
        UserModel userModel = super.getSessionUser(request);
        if (userModel.getShopId() == null) {
            throw new BusinessException("您还没有商铺");
        }
        PageInfo pageInfo = orderService.listByShopId(userModel.getShopId(), pageNum, pageSize);
        return ResponseVoUtil.success(new PageResponseBean<>(pageInfo));
    }

    @ApiOperation(value = "派送员查看订单列表", notes = "派送员查看订单列表")
    @GetMapping("/list")
    public ResponseVO<List<OrderModel>> list(@RequestParam Integer shopId, HttpServletRequest request) {
        UserModel userModel = super.getSessionUser(request);
        if (userModel.getShopId() == null) {
            throw new BusinessException("您还没有商铺");
        }
        List<OrderModel> orderModels = orderService.listUnfilledAndUnreceivedByShopId(userModel.getShopId());
        return ResponseVoUtil.success(orderModels);
    }

    @ApiOperation(value = "查看订单列表根据状态", notes = "查看订单")
    @GetMapping("/listByStatus")
    public ResponseVO<List<OrderModel>> listByStatus(@RequestParam Integer shopId, @RequestParam Byte status, HttpServletRequest request) {
        UserModel userModel = super.getSessionUser(request);
        if (userModel.getShopId() == null) {
            throw new BusinessException("您还没有商铺");
        }
        List<OrderModel> orderModels = orderService.listByShopIdAndStatus(userModel.getShopId(), status);
        return ResponseVoUtil.success(orderModels);
    }


    @ApiOperation(value = "查看订单详情", notes = "查看订单详情")
    @GetMapping("/get")
    public ResponseVO<OrderModel> get(@RequestParam String orderId, HttpServletRequest request) {
        OrderModel orderModel = checkOrderOwner(orderId, request);
        return ResponseVoUtil.success(orderModel);
    }

    @ApiOperation(value = "接受订单", notes = "接收订单")
    @PostMapping("/receive")
    public ResponseVO<OrderModel> reveive(@RequestParam String orderId, HttpServletRequest request) {
        OrderModel orderModel = checkOrderOwner(orderId, request);
        orderModel = orderService.receive(orderModel);
        return ResponseVoUtil.success(orderModel);
    }

    @ApiOperation(value = "取消订单", notes = "取消订单")
    @PostMapping("/cancel")
    public ResponseVO<OrderModel> cancel(@RequestParam String orderId, HttpServletRequest request) {
        OrderModel orderModel = checkOrderOwner(orderId, request);
        orderModel = orderService.cancel(orderModel);
        return ResponseVoUtil.success(orderModel);
    }

    @ApiOperation(value = "完结订单", notes = "完结订单")
    @PostMapping("/finish")
    public ResponseVO<OrderModel> finish(@RequestParam String orderId, HttpServletRequest request) {
        OrderModel orderModel = checkOrderOwner(orderId, request);
        orderModel = orderService.finish(orderModel);
        return ResponseVoUtil.success(orderModel);
    }


    private OrderModel checkOrderOwner(String orderId, HttpServletRequest request) {
        UserModel userModel = super.getSessionUser(request);
        OrderModel orderModel = orderService.get(orderId);
        if (orderModel == null) {
            throw new BusinessException(Errors.ORDER_NOT_EXIST);
        }
        // 校验商铺是否归属该用户的
        if (!orderModel.getShopId().equals(userModel.getShopId())) {
            throw new BusinessException(Errors.SYSTEM_NO_ACCESS);
        }
        return orderModel;
    }
}
